/*
 * @Author: YWY
 * @Date: 2022-02-28 23:32:39
 * @LastEditors: YWY
 * @LastEditTime: 2022-03-05 19:41:40
 */

import { defineStore } from "pinia";
import { getKeywordAssociation, followUser, disFollowUser, getArtcileSearch, getUserSearch } from '@/api/services/user.service.js'
import { Toast } from "vant";

export const searchStore = defineStore('search', {
    state: () => {
        return {
            inputValue: "",
            searchValue: " ",
            articleList: [],
            userList: [],
            association: false,
            associationList: [],
            currentPage: 1,
            finished: false
        }
    },
    actions: {
        // 获取关键词联想列表
        loadKeywordAssociation(val) {
            if (val && !/^\s*$/.test(val)) {
                console.log("发送模糊关键词联想");
                getKeywordAssociation(val).then((res) => {
                    if (res.g) {
                        console.log("接收到了关键词联想信息");
                        if (this.inputValue && !/^\s*$/.test(this.inputValue)) {
                            this.$patch(
                                {
                                    association: true,
                                    associationList: res.g
                                }
                            )
                        }
                    } else {
                        console.log("没有接收到关键词联想信息");
                        this.$patch(
                            {
                                association: false,
                                associationList: []
                            }
                        )
                    }
                });
            }
            else {
                this.association = false;
            }
        },

        // 初始化搜索请求并接受数据
        async init(val) {
            // 同步searchValue
            this.searchValue = val;
            // 获取文章搜索数据
            let result = await getArtcileSearch(val);
            let data1 = result.result.res.rows;
            // 获取用户搜索数据
            result = await getUserSearch(val);
            let data2 = result.result.users;
            this.$patch({
                articleList: data1,
                userList: data2,
                currentPage: this.currentPage + 1
            })
        },

        //上拉加载更多数据
        async loadArticleList(val) {
            console.log("加载更多数据")
            let result = await getArtcileSearch(val, this.currentPage, 10)
            let data = result.result.res.rows;
            if (!data.length) {
                console.log(data.length)
                this.finished = true
            }
            this.articleList = this.articleList.concat(data);
        },

        //关注功能
        addFollow(id) {
            followUser(id).then((res) => {
                Toast("关注成功");
                this.userList.some((item) => {
                    if (item.id === id) {
                        console.log(item.follow)
                        item.follow = true;
                        console.log(item.follow)
                        return true
                    }
                })
            });
        },

        //取消关注
        disFollow(id) {
            disFollowUser(id).then((res) => {
                Toast("取消关注成功");
                this.userList.some((item) => {
                    if (item.id === id) {
                        console.log(item.follow)
                        item.follow = false;
                        console.log(item.follow)
                        return true
                    }
                })
            });
        }
    }
})